##########################################################
# John Henderson and Alex Theodoridis
# Replication Data for: "Seeing Spots", 
#  Forthcoming in Political Behavior, August 20, 2017
# 
##########################################################
#
#  figure_3.R + figure_iv.R
#  -- file produces the density plots Figure 3
#
##########################################################

rm(list=ls())
source('~/Dropbox/Seeing_Spots/replication/pre_data.R')
                 
newsint_old = newsint - 2 
newsint_alt = rowSums(cbind(sen_majority_correct,hou_majority_correct))-1 
#newsint[which(newsint<0)] = -1
#newsint[which(newsint>0)] =  1

college=as.numeric(cces_data$educ)
college[which(as.numeric(cces_data$educ)<3)]=-1
college[which(as.numeric(cces_data$educ)==3 | as.numeric(cces_data$educ)==4)]=0
college[which(as.numeric(cces_data$educ)>4)]=1

newsint_edu = college

# density plots - interesteds

completes = !is.na(time_watched) & !is.na(pid_lean) & !is.na(newsint_old)

ytime 			= time_watched[completes] 
tr_pos_ad 		= pos_ad[completes] 
tr_neg_ad    	= neg_ad[completes] 
tr_obama_ad   	= obama_ad[completes] 
tr_romney_ad  	= romney_ad[completes] 
tr_obama_pos	= pos_ad[completes] 
tr_obama_neg  	= neg_ad[completes] 
tr_romney_pos 	= obama_ad[completes] 
tr_romney_neg 	= romney_ad[completes] 
pid				= pid_lean[completes] 
newsint_alt		= newsint_alt[completes] 
newsint_old		= newsint_old[completes] 
newsint_edu		= newsint_edu[completes] 

#lows = newsint_old==-1
#meds = newsint_old== 0
#high = newsint_old== 1       

lows = newsint_alt==-1
meds = newsint_alt== 0
high = newsint_alt== 1
                     
dems = pid==-1
inds = pid== 0
reps = pid== 1    
          

# cands by pid 
# dem pid
{            
pdf(file='~/Dropbox/Seeing_Spots/replication/figures/density_cand_by_dem_pid_both.pdf') 
# obama v. romney; by dem pid
	plot(density(c(ytime[dems & tr_romney_ad]),from=0,to=55),main='',xlab='Time Watched',ylim=c(0,.09),col='black',lwd=2)  
	lines(density(ytime[c(dems & tr_obama_ad)],from=0,to=55),main='',xlab='Time Watched',lty=2,col='black',lwd=2)      
	legend(cex=1.45,x=c(1),y=c(.08),legend=c('Romney Ad'),lty=c(1),box.col="#0000ff00",col='black',lwd=2)  
	legend(cex=1.45,x=c(1),y=c(.075),legend=c('Obama Ad'),lty=c(2),box.col="#0000ff00",col='black',lwd=2)
	ksp=round(ks.test(c(ytime[dems & tr_romney_ad]),ytime[c(dems & tr_obama_ad)])$p.value,digits=3)
	tp=round(t.test(c(ytime[dems & tr_romney_ad]),ytime[c(dems & tr_obama_ad)])$p.value,digits=3)  

	text(cex=1.45,x=c(47),y=c(.085),label=c(expression(italic(paste(p,' = ', 0.428),sep=''))),
	  col='black',lwd=2)
dev.off()	
}
            
# rep pid
{  
pdf(file='~/Dropbox/Seeing_Spots/replication/figures/density_cand_by_rep_pid_both.pdf')  
# obama v. romney; by rep pid
	plot(density(c(ytime[reps & tr_romney_ad]),from=0,to=55),main='',xlab='Time Watched',ylim=c(0,.09),col='black',lwd=2)    
	lines(density(ytime[c(reps & tr_obama_ad)],from=0,to=55),main='',xlab='Time Watched',lty=2,col='black',lwd=2)            
	legend(cex=1.45,x=c(1),y=c(.08),legend=c('Romney Ad'),lty=c(1),box.col="#0000ff00",col='black',lwd=2)   
	legend(cex=1.45,x=c(1),y=c(.075),legend=c('Obama Ad'),lty=c(2),box.col="#0000ff00",col='black',lwd=2)     
	ksp=round(ks.test(c(ytime[reps & tr_romney_ad]),ytime[c(reps & tr_obama_ad)])$p.value,digits=3)
	tp=round(t.test(c(ytime[reps & tr_romney_ad]),ytime[c(reps & tr_obama_ad)])$p.value,digits=3)  

	text(cex=1.45,x=c(47),y=c(.085),label=c(expression(italic(paste(p,' = ', 0.018),sep=''))),
	  col='black',lwd=2)
dev.off()
}
    
# in figure iv in appendix
# ind pid
{  
pdf(file='~/Dropbox/Seeing_Spots/replication/figures/density_cand_by_ind_pid_both.pdf')  
# obama v. romney; by ind pid
	plot(density(c(ytime[inds & tr_romney_ad]),from=0,to=55),main='',xlab='Time Watched',ylim=c(0,.09),col='black',lwd=2)    
	lines(density(ytime[c(inds & tr_obama_ad)],from=0,to=55),main='',xlab='Time Watched',lty=2,col='black',lwd=2)            
	legend(cex=1.45,x=c(1),y=c(.08),legend=c('Romney Ad'),lty=c(1),box.col="#0000ff00",col='black',lwd=2)   
	legend(cex=1.45,x=c(1),y=c(.075),legend=c('Obama Ad'),lty=c(2),box.col="#0000ff00",col='black',lwd=2)     
	ksp=round(ks.test(c(ytime[inds & tr_romney_ad]),ytime[c(inds & tr_obama_ad)])$p.value,digits=3)
	tp=round(t.test(c(ytime[inds & tr_romney_ad]),ytime[c(inds & tr_obama_ad)])$p.value,digits=3)  

	text(cex=1.45,x=c(47),y=c(.085),label=c(expression(italic(paste(p,' = ', 0.046),sep=''))),
	  col='black',lwd=2)
dev.off()
}
# END figure_3_iv.R